﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.SignalR;

namespace UI.Hubs
{
    public class SingarFilterAttribute : IAsyncActionFilter
    {
        private readonly IHubContext<ChatHub> hubContext;
        private readonly string title;

        public SingarFilterAttribute(IHubContext<ChatHub> hubContext, string title)
        {
            this.hubContext = hubContext;
            this.title = title;
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            await next.Invoke();
            //发送消息
            SingarFilterAttribute sf = (SingarFilterAttribute)context.Filters.FirstOrDefault(e => e.GetType() == typeof(SingarFilterAttribute));
            await hubContext.Clients.All.SendAsync("ReceiveMessage", sf.title, "新增1条审核信息");
        }
    }
}
